package org.idea.qiyu.framework.datasource.config;

import org.idea.qiyu.framework.datasource.properties.MultiDataSourceProperties;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import javax.annotation.Resource;

/**
 * @Author linhao
 * @Date created in 11:59 上午 2021/5/25
 */
public class DynamicDataSource extends AbstractRoutingDataSource {

    @Resource
    private MultiDataSourceProperties multiDataSourceProperties;

    @Override
    protected Object determineCurrentLookupKey() {
        String datasource = DataSourceContextHolder.getDatasource();
        if (datasource == null || "".equals(datasource)) {
            return multiDataSourceProperties.getDefaultDatasource();
        }
        return datasource;
    }
}
